<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input {
            outline: none;
            font-size: 20px;
        }
        ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .msg-box {
            width: 600px;
            height: 400px;
            border: 2px solid #aaa;
            box-sizing: border-box;
            padding: 10px;
            overflow: auto;
        }

        .send-info-box {
            display: flex;
            flex-direction: column;
            width: 600px;
            height: 100px;
            border: 2px solid #aaa;
            box-sizing: border-box;
            margin-top: 10px;
        }

        .xiaoming {
            width: 100%;
            height: 50px;
            display: flex;
            justify-content: space-between;
        }

        .xiaoming input {
            flex: 1;
        } 
        .xiaoming button {
            width: 100px;
        } 

        .xiaohong {
            width: 100%;
            height: 50px;
            display: flex;
            justify-content: space-between;
        }
        .xiaohong input {
            flex: 1;
        } 
        .xiaohong button {
            width: 100px;
        } 

        .xm-msg {
            text-align: left;
            color: blue
        }

        .xh-msg {
            text-align: right;
            color: pink
        }
    </style>
</head>
<body>
    <input type="text" placeholder="请输入关键字" class="search-input">

    <div class="msg-box">
        <ul></ul>
    </div>

    <div class="send-info-box">
        <div class="xiaoming">
            <input type="text">
            <button id="xm-btn" type="">小明说</button>
        </div>
        <div class="xiaohong">
            <input type="text">
            <button id="xh-btn" type="">小红说</button>
        </div>
    </div>


<script>
    // 小红发的信息----文字颜色为粉色-右对齐
    // 小明发的信息----文字颜色为蓝色-左对齐

    let allMsgList = []
    let showMsgList = []
    let ul = document.querySelector('.msg-box ul')
    let searchInput = document.querySelector('.search-input')
    let createMsg = function () {
        showMsgList = allMsgList.filter(item => {
            return item.msg.includes(searchInput.value)
        })

        let domStr = showMsgList.map(item => {
            return `
                <li id="${item.id}" class="${item.type === 'xm' ? 'xm-msg' : 'xh-msg'}">
                    ${item.type === 'xm' ? '小明说' : '小红说'}:${item.msg}
                </li>`
        })

        ul.innerHTML = domStr.join('')

        let lis = document.querySelectorAll('.msg-box ul li')

        for (let i = 0; i < lis.length; i++) {
            lis[i].ondblclick = function (event) {
                console.log(event.target.id)

                // findIndex

                let index = allMsgList.findIndex(item => {
                    return Number(item.id) === Number(event.target.id)
                })

                allMsgList.splice(index, 1)

                createMsg()

                console.log('查找到的下表', index)
            }
        }
    }

    let filterMsg = function (event) {
        if (event.keyCode === 13) {
            let filterStr = event.target.value
            let filterArr = allMsgList.filter(item => {
                return item.msg.includes(filterStr)
            })

            showMsgList = filterArr

            createMsg()
            console.log(filterArr)
        }
    }

    document.querySelector('.search-input').onkeyup = filterMsg


    document.getElementById('xm-btn').onclick = xmSay

    document.querySelector('.xiaoming input').onkeyup = function (event) {
        if (event.keyCode === 13) {
            xmSay(event)
        }
    }

    document.querySelector('.xiaohong input').onkeyup = function (event) {
        if (event.keyCode === 13) {
            xhSay(event)
        }
    }

    document.getElementById('xh-btn').onclick = xhSay

    function xmSay (event) {
        let xmInput = document.querySelector('.xiaoming input')
        
        allMsgList.push({
            msg: xmInput.value,
            type: 'xm',
            id: new Date().getTime()
        })
    
        xmInput.value = ''

        createMsg()
    }

    function xhSay (event) {
        let xhInput = document.querySelector('.xiaohong input')

        allMsgList.push({
            msg: xhInput.value,
            type: 'xh',
            id: new Date().getTime()
        })

        xhInput.value = ''
    
        createMsg()
    }
</script>

</body>
</html>